﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.ShpMana
{
    public partial class wfUpdateRegionalAgent : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 获取银行类型
        /// </summary>
        public void LoadBankType()
        {
            using (CBizBseBank bseBank = new CBizBseBank())
            {
                DataTable dt = bseBank.GetBseBankList();
                if (dt != null)
                {
                    string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 100, "");

                    this.ExecJScript(@"
                            var _html = $('#svc_list_temp_bseBank').html();
                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#bankType').html(render);
                                $('#svc_list_temp_bseBank').remove();
                            });");
                }
            }

        }


        /// <summary>
        /// 修改 区域代理
        /// </summary>
        public void SaveData()
        {
            //消息验证
            #region
            string userid = this.GetParamHtValue("userid");
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('无效用户.', 3);");
                return;
            }
            string userPric = this.GetParamHtValue("userPric");
            if (string.IsNullOrEmpty(userPric))
            {
                this.ExecJScript(@"layer.alert('请选择区域代理头像.', 3);");
                return;
            }
            string identityCard = this.GetParamHtValue("identityCard");
            if (string.IsNullOrEmpty(identityCard))
            {
                this.ExecJScript(@"layer.alert('请添加区域代理身份证.', 3);");
                return;
            }
            string credentialsNo = this.GetParamHtValue("credentialsNo");
            if (string.IsNullOrEmpty(credentialsNo))
            {
                this.ExecJScript(@"layer.alert('请添加身份证号码.', 3);");
                return;
            }
            string realName = this.GetParamHtValue("realName");
            if (string.IsNullOrEmpty(realName))
            {
                this.ExecJScript(@"layer.alert('请填写真实姓名.', 3);");
                return;
            }
            string Gender = this.GetParamHtValue("Gender");
            string birth = this.GetParamHtValue("birth");
            if (string.IsNullOrEmpty(birth))
            {
                this.ExecJScript(@"layer.alert('请添加生日.', 3);");
                return;
            }
            string selCountry = this.GetParamHtValue("selCountry");
            if (string.IsNullOrEmpty(selCountry))
            {
                this.ExecJScript(@"layer.alert('请选择国家.', 3);");
                return;
            }
            string selCity = this.GetParamHtValue("selCity");
            if (string.IsNullOrEmpty(selCity))
            {
                this.ExecJScript(@"layer.alert('请选择城市.', 3);");
                return;
            }
            string selDestination = this.GetParamHtValue("selDestination");
            if (string.IsNullOrEmpty(selDestination))
            {
                this.ExecJScript(@"layer.alert('请选择地区.', 3);");
                return;
            }
            string mobile = this.GetParamHtValue("mobile");
            if (string.IsNullOrEmpty(mobile))
            {
                this.ExecJScript(@"layer.alert('请填写电话号码.', 3);");
                return;
            }
            string bankType = this.GetParamHtValue("bankType");
            if (string.IsNullOrEmpty(bankType))
            {
                this.ExecJScript(@"layer.alert('请选择银行类型.', 3);");
                return;
            }
            string bankAccount = this.GetParamHtValue("bankAccount");
            if (string.IsNullOrEmpty(bankAccount))
            {
                this.ExecJScript(@"layer.alert('请填写银行卡号.', 3);");
                return;
            }
            #endregion

            string phone = this.GetParamHtValue("phone");
            string qq = this.GetParamHtValue("qq");
            string wechatNo = this.GetParamHtValue("wechatNo");
            string nick = realName.Substring(0, 1) + "师傅";

            string credentialsType = this.GetParamHtValue("credentialsType");
            string pwd = mobile.Substring(5, 6);

            using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
            {
                //保存到系统永久目录，之前上传时存到临时目录的，也可另写代码拷贝文件到其他目录
                identityCard = oCBizSysFile.SaveImgToServer("", msBsc.SvRef.UserInfo.Operator, "1", "VEH", ""
                    , identityCard//文件来源虚拟路径
                    , ""//文件来源物理路径，虚拟和物理只有一个就行
                    , true);
            }

            //修改
            using (CBizCustomerRegister cr = new CBizCustomerRegister())
            {
                string[] message = cr.UpdateProxyRegister(userid, userPric, identityCard, realName, birth, credentialsNo, selCountry, selCity, selDestination, phone, mobile, qq, wechatNo, bankType, bankAccount, Gender, nick, credentialsType);
                if (!Convert.ToBoolean(message[0]))
                {
                    this.ExecJScript(@"layer.alert('" + message[1] + "', 3);"); return;
                }

                this.ExecJScript(@"success();");
            }
        }

        /// <summary>
        /// 绑定 用户信息
        /// </summary>
        public void BindData()
        {
            string userid = this.GetParamHtValue("userid");
            if (string.IsNullOrEmpty(userid))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo userInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                DataTable dt = userInfo.GetBaseInfoByID(userid);

                if (dt != null)
                {
                    string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 2, "");
                    this.ExecJScript("bindData(" + ret + ");");
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "changeArea")
            {
                RefreshArea();
            }
            else if (this.strAction == "bankType")
            {
                LoadBankType();
            }
            else if (this.strAction == "save")
            {
                SaveData();
            }
            else if (this.strAction == "getInfo")
            {
                BindData();
            }
        }
        /// <summary>
        /// 地区联动
        /// </summary>
        private void RefreshArea()
        {
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");

            DataSet ds1, ds2;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds1 = string.IsNullOrEmpty(countryId) ? null : objCBizCountryCity.SelectCountryByParentId(countryId);

                ds2 = string.IsNullOrEmpty(cityId) ? null : objCBizCountryCity.SelectCountryByParentId(cityId);
            }

            if (ds1 != null)
            {
                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds1.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#country_city_list').html();
                            laytpl(_html).render(" + countryJson + @", function (render) {
                                $('#selCity').html(render);
                            });");
            }

            if (ds2 != null)
            {
                var cityJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds2.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#city_des_list').html();
                            laytpl(_html).render(" + cityJson + @", function (render) {
                                $('#selDestination').html(render);
                            });");
            }
        }
    }
}